//
//  ViewExtension.swift
//  AICleaner
//
//  Created by firefly on 2025/1/25.
//

import Foundation
import SwiftUI

struct RoundedCorner: Shape {
    var radius: CGFloat
    var corners: UIRectCorner
    
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect,byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        
        return Path(path.cgPath)
    }
}

extension View {
    func borderRadius(_ radius: CGFloat, corners: UIRectCorner = .allCorners) -> some View{
        clipShape(RoundedCorner(radius: radius, corners: corners))
    }
}

extension UIApplication {
    /// 获取window
    var currentWindow: UIWindow? {
        // 获取所有连接的场景
        let connectedScenes = UIApplication.shared.connectedScenes
        // 筛选出处于前台活动状态的场景
        let activeScene = connectedScenes.first { $0.activationState == .foregroundActive } as? UIWindowScene
        // 获取当前的主窗口
        return activeScene?.windows.first { $0.isKeyWindow }
    }
}
